package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPicOverBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.apis.ApiManager;
import stark.common.base.IReqRetCallback;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class PicOverActivity extends BaseAc<ActivityPicOverBinding> {

    public static String imgPath = "";
    private String path1 = "";
    private String path2 = "";
    private String path3 = "";
    private ImageView selView;
    private int selPos = 1;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_over;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicOverBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvPicOverSave.setOnClickListener(this);
        mDataBinding.ivPicOverAdd.setOnClickListener(this);
        mDataBinding.ivPicOverSelImg1.setOnClickListener(this);
        mDataBinding.ivPicOverSelImg2.setOnClickListener(this);
        mDataBinding.ivPicOverSelImg3.setOnClickListener(this);
        mDataBinding.ivPicOverDel1.setOnClickListener(this);
        mDataBinding.ivPicOverDel2.setOnClickListener(this);
        mDataBinding.ivPicOverDel3.setOnClickListener(this);
        mDataBinding.tvPicOverMatt.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPicOverSave://保存
                saveImg();
                break;
            case R.id.ivPicOverAdd://添加图片
                Intent intent = new Intent(this, SelPictureActivity.class);
                intent.putExtra(Content.Again, "Again");
                startActivityForResult(intent, Content.OVER_ADD);
                break;
            case R.id.ivPicOverSelImg1://选中图片1
                clearView();
                mDataBinding.tvPicOverSel1.setVisibility(View.VISIBLE);
                mDataBinding.sbPicOver1.setVisibility(View.VISIBLE);
                selView = mDataBinding.ivPicOverImg1;
                selPos = 0;
                break;
            case R.id.ivPicOverSelImg2://选中图片2
                setDef2();
                break;
            case R.id.ivPicOverSelImg3://选中图片3
                clearView();
                mDataBinding.tvPicOverSel3.setVisibility(View.VISIBLE);
                mDataBinding.sbPicOver3.setVisibility(View.VISIBLE);
                selView = mDataBinding.ivPicOverImg3;
                selPos = 2;
                break;
            case R.id.ivPicOverDel1://删除图片1
                path1 = "";
                mDataBinding.ivPicOverDel1.setVisibility(View.GONE);
                mDataBinding.ivPicOverImg1.setVisibility(View.GONE);
                mDataBinding.ivPicOverSelImg1.setVisibility(View.GONE);
                setDef2();
                break;
            case R.id.ivPicOverDel2://删除图片2
                deleteImg2();
                break;
            case R.id.ivPicOverDel3://删除图片3
                path3 = "";
                mDataBinding.rlPicOverView3.setVisibility(View.GONE);
                mDataBinding.ivPicOverImg3.setVisibility(View.GONE);
                setDef2();
                break;
            case R.id.tvPicOverMatt://人像抠图
                distHuman();
                break;
        }
    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(imgPath)) return;
        path2 = imgPath;
        Glide.with(this).load(path2).into(mDataBinding.ivPicOverImg2);
        Glide.with(this).load(path2).into(mDataBinding.ivPicOverSelImg2);
        selView = mDataBinding.ivPicOverImg2;
        setAlpha(90);//默认设置图片2透明度
        //图片1透明度
        mDataBinding.sbPicOver1.setMax(90);
        mDataBinding.sbPicOver1.setProgress(90);
        mDataBinding.sbPicOver1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                setAlpha(seekBar.getProgress() * 1f);
            }
        });
        //图片2透明度
        mDataBinding.sbPicOver2.setMax(90);
        mDataBinding.sbPicOver2.setProgress(90);
        mDataBinding.sbPicOver2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                setAlpha(seekBar.getProgress() * 1f);
            }
        });
        //图片3透明度
        mDataBinding.sbPicOver3.setMax(90);
        mDataBinding.sbPicOver3.setProgress(90);
        mDataBinding.sbPicOver3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                setAlpha(seekBar.getProgress() * 1f);
            }
        });
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlPicOverSave);
                ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                //保存到创作记录
                String resultFile = FileUtil.generateFilePath(Content.myCreate, ".png");
                ImageUtils.save(bitmap, resultFile, Bitmap.CompressFormat.PNG);
                emitter.onNext(resultFile);
            }

            @Override
            public void accept(String path) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        PicOverResultActivity.imgPath = path;
                        startActivity(PicOverResultActivity.class);
                    }
                }, 1000);
            }
        });
    }

    //设置透明度
    private void setAlpha(float tran) {
        float newAlpha = (tran + 10) / 100;
        selView.setAlpha(newAlpha);
    }

    //识别人像
    private void distHuman() {
        mDataBinding.tvPicOverTip.setText(R.string.matt_ing);
        mDataBinding.tvPicOverTip.setVisibility(View.VISIBLE);
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                Bitmap bitmap = ImageUtils.view2Bitmap(selView);
                //保存到创作记录
                String resultFile = FileUtil.generateFilePath(Content.myCreate, ".png");
                ImageUtils.save(bitmap, resultFile, Bitmap.CompressFormat.PNG);
                emitter.onNext(resultFile);
            }

            @Override
            public void accept(String path) {
                ApiManager.humanApi().hmBodySegBmp(PicOverActivity.this, UriUtil.path2Uri(mContext, path), new IReqRetCallback<Bitmap>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                        if (success && result != null) {
                            RxUtil.create(new RxUtil.Callback<String>() {

                                @Override
                                public void doBackground(ObservableEmitter<String> emitter) {
                                    //保存到创作记录
                                    String resultFile = FileUtil.generateFilePath(Content.myCreate, ".png");
                                    ImageUtils.save(result, resultFile, Bitmap.CompressFormat.PNG);
                                    emitter.onNext(resultFile);
                                }

                                @Override
                                public void accept(String path) {
                                    switch (selPos) {
                                        case 0:
                                            path1 = path;
                                            Glide.with(mContext).load(path1).into(mDataBinding.ivPicOverImg1);
                                            Glide.with(mContext).load(path1).into(mDataBinding.ivPicOverSelImg1);
                                            mDataBinding.sbPicOver1.setProgress(90);
                                            break;
                                        case 1:
                                            path2 = path;
                                            Glide.with(mContext).load(path2).into(mDataBinding.ivPicOverImg2);
                                            Glide.with(mContext).load(path2).into(mDataBinding.ivPicOverSelImg2);
                                            mDataBinding.sbPicOver2.setProgress(90);
                                            break;
                                        case 2:
                                            path3 = path;
                                            Glide.with(mContext).load(path3).into(mDataBinding.ivPicOverImg3);
                                            Glide.with(mContext).load(path3).into(mDataBinding.ivPicOverSelImg3);
                                            mDataBinding.sbPicOver3.setProgress(90);
                                            break;
                                    }
                                    tipResult(getString(R.string.matt_suc));
                                }
                            });
                        } else {
                            tipResult(getString(R.string.matt_def));
                        }
                    }
                });
            }
        });
    }

    //提示是否成功
    private void tipResult(String text) {
        mDataBinding.tvPicOverTip.setText(text);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mDataBinding.tvPicOverTip.setVisibility(View.GONE);
            }
        }, 500);
    }

    //默认选择图片2
    private void setDef2() {
        clearView();
        mDataBinding.tvPicOverSel2.setVisibility(View.VISIBLE);
        mDataBinding.sbPicOver2.setVisibility(View.VISIBLE);
        selView = mDataBinding.ivPicOverImg2;
        selPos = 1;
    }

    //管理kind选择
    private void clearView() {
        mDataBinding.tvPicOverSel1.setVisibility(View.GONE);
        mDataBinding.tvPicOverSel2.setVisibility(View.GONE);
        mDataBinding.tvPicOverSel3.setVisibility(View.GONE);
        mDataBinding.sbPicOver1.setVisibility(View.GONE);
        mDataBinding.sbPicOver2.setVisibility(View.GONE);
        mDataBinding.sbPicOver3.setVisibility(View.GONE);
    }

    //删除图片2
    private void deleteImg2() {
        if (TextUtils.isEmpty(path1)) {
            if (TextUtils.isEmpty(path3)) {
                ToastUtils.showShort(R.string.please_mast_save_one_img);
            } else {
                path2 = path3;
                path3 = "";
                Glide.with(this).load(path2).into(mDataBinding.ivPicOverImg2);
                Glide.with(this).load(path2).into(mDataBinding.ivPicOverSelImg2);
                mDataBinding.rlPicOverView3.setVisibility(View.GONE);
            }
        } else {
            path2 = path1;
            path1 = "";
            Glide.with(this).load(path2).into(mDataBinding.ivPicOverImg2);
            Glide.with(this).load(path2).into(mDataBinding.ivPicOverSelImg2);
            mDataBinding.ivPicOverDel1.setVisibility(View.GONE);
            mDataBinding.ivPicOverImg1.setVisibility(View.GONE);
            mDataBinding.ivPicOverSelImg1.setVisibility(View.GONE);
        }
        setDef2();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Content.OVER_ADD) {
            String path = data.getStringExtra(Content.imagePath);
            if (path == null || TextUtils.isEmpty(path)) return;
            if (TextUtils.isEmpty(path3)) {
                path3 = path;
                Glide.with(this).load(path3).into(mDataBinding.ivPicOverImg3);
                Glide.with(this).load(path3).into(mDataBinding.ivPicOverSelImg3);
                mDataBinding.tvPicOverSel3.setVisibility(View.GONE);
                mDataBinding.rlPicOverView3.setVisibility(View.VISIBLE);
                mDataBinding.ivPicOverImg3.setVisibility(View.VISIBLE);
                mDataBinding.sbPicOver3.setProgress(90);
            } else {
                path1 = path;
                Glide.with(this).load(path1).into(mDataBinding.ivPicOverImg1);
                Glide.with(this).load(path1).into(mDataBinding.ivPicOverSelImg1);
                mDataBinding.ivPicOverDel1.setVisibility(View.VISIBLE);
                mDataBinding.ivPicOverImg1.setVisibility(View.VISIBLE);
                mDataBinding.ivPicOverSelImg1.setVisibility(View.VISIBLE);
                mDataBinding.sbPicOver1.setProgress(90);
            }
        }
    }

}